package com.wars2d.client;

import org.lwjgl.Sys;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class PlayerActor extends Actor {

	TextureRegion mRegion = null;
	
	final float gravity = 1000.0f;
	
	float forceY = 0.0f;
	
	float velocityY = 0.0f;

	private GameStage gameStage;
	
	public boolean onGround = true;
	
	boolean jumping = false;
	
	public PlayerActor(GameStage stage, TextureRegion texture) {
		mRegion = texture;
		
		gameStage = stage;
		
		width 	= texture.getRegionWidth();
		height 	= texture.getRegionHeight();
	}
	
	@Override
	public void draw(SpriteBatch arg0, float arg1) {
		arg0.draw(mRegion, x, y);
	}

	@Override
	public Actor hit(float arg0, float arg1) {
		return null;
	}
	
	@Override
	public void act(float delta) {
		super.act(delta);
		
//		int gridY = (int) Math.round(y / gameStage.pixelYPerBlock);
//		
//		float deltaY = y - gridY * gameStage.pixelYPerBlock;
//		
//		int closesGroundX = (int) Math.round(x / gameStage.pixelXPerBlock);
//		
//		if (gameStage.isGround(closesGroundX, gridY - 1) && velocityY <= 0 && deltaY <= 5f && deltaY >= 0.0f) {
//			onGround = true;
//			y = gridY * gameStage.pixelYPerBlock;
//			forceY = 0;
//			velocityY = 0;
//		} else {
//			onGround = false;
//			forceY = gravity;
//		}
//		
//		velocityY += forceY * delta;
//		
//		y += velocityY * delta;
		
		velocityY -= gravity * delta;
		
		if (gameStage.canGo(x, y + delta * velocityY, width, height)) {
			y += delta * velocityY;
		} else {
			velocityY = 0;
			onGround = true;
		}
		
	}

	public void tryJump() {
		if (onGround) {
			velocityY = 2 * gravity;
			onGround = false;
		}
	}

	public void goToLeft(float deltaX, float deltaTime) {
//		int gridX = (int) Math.round(x / gameStage.pixelXPerBlock);
//		int gridY = (int) Math.round(y / gameStage.pixelYPerBlock);
//		
//		System.out.println("gridX : " + gridX + " gridY : " + gridY);
//		
//		if (gameStage.isGround(gridX, gridY)) {
////			x = gridX * gameStage.pixelXPerBlock;
//			x -= deltaX * deltaTime;
//		}
		
		if (gameStage.canGo(x - deltaX * deltaTime, y, width, height)) {
			x -= deltaX * deltaTime;
		}
	}
	
	public void goToRight(float deltaX, float deltaTime) {
//		x += deltaX * deltaTime;
//		
//		int gridX = (int) Math.floor(x / gameStage.pixelXPerBlock) + 1;
//		int gridY = (int) Math.floor(y / gameStage.pixelYPerBlock);
//		
//		System.out.println("gridX : " + gridX + " gridY : " + gridY);
//		
//		if (gameStage.isGround(gridX, gridY)) {
////			x = gridX * gameStage.pixelXPerBlock;
//			x -= deltaX * deltaTime;
//		}
		if (gameStage.canGo(x + deltaX * deltaTime, y, width, height)) {
			x += deltaX * deltaTime;
		}
	} 


}
